package com.gddb.xwj.mywiki.service;

import com.gddb.xwj.mywiki.domain.Category;
import com.gddb.xwj.mywiki.domain.CategoryExample;
import com.gddb.xwj.mywiki.mapper.CategoryMapper;
import com.gddb.xwj.mywiki.req.CategoryQueryReq;
import com.gddb.xwj.mywiki.req.CategorySaveReq;
import com.gddb.xwj.mywiki.resp.CategoryQueryResp;
import com.gddb.xwj.mywiki.resp.PageResp;
import com.gddb.xwj.mywiki.util.CopyUtil;
import com.gddb.xwj.mywiki.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述：分类管理 （直接电子书管理中复制）
 *
 * @author xwj
 * @create 2022-03-19 17:53
 */
@Service
@Slf4j
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;

    /**
     * 全量查询 额外加个顺序升序
     * @return
     */
    public List<CategoryQueryResp> all(CategoryQueryReq req) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        // 列表复制
        List<CategoryQueryResp> list = CopyUtil.copyList(categoryList, CategoryQueryResp.class);

        return list;
    }

    /**
     * 分页查询
     * @param req
     * @return
     */
    public PageResp<CategoryQueryResp> list(CategoryQueryReq req){
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }
        //只会对第一个遇到的select生效
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        // 列表复制
        List<CategoryQueryResp> list = CopyUtil.copyList(categoryList, CategoryQueryResp.class);

        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        //注意点 PageInfo<CategoryQueryResp> pageInfo = new PageInfo<>(list); 如果pageinfo用的是CategoryQueryResp 则返回的是分页的总数 必须为category才是全部
        log.info("总行数 :{}", pageInfo.getTotal());
        log.info("总页数 :{}", pageInfo.getPages());

        PageResp<CategoryQueryResp> pageResp = new PageResp();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);

        return pageResp;
    }

    /**
     * 保存
     * @param req
     */
    public void save(CategorySaveReq req) {
        Category category = CopyUtil.copy(req, Category.class);
        if(ObjectUtils.isEmpty(req.getId())){
            //新增
            category.setId(snowFlake.nextId());
            categoryMapper.insert(category);
        }else{
            //修改
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    /**
     * 删除
     * @param id
     */
    public void delete(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }
}
